package com.komorebi.friends.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.komorebi.friends.common.ErrorCode;
import com.komorebi.friends.exception.BusinessException;
import com.komorebi.friends.model.entity.Team;
import com.komorebi.friends.model.entity.User;
import com.komorebi.friends.model.entity.UserTeam;
import com.komorebi.friends.model.vo.TeamVO;
import com.komorebi.friends.model.vo.UserVO;
import com.komorebi.friends.service.TeamService;
import com.komorebi.friends.service.UserService;
import com.komorebi.friends.service.UserTeamService;
import com.komorebi.friends.mapper.UserTeamMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author LX-MF
* @description 针对表【user_team(用户 - 队伍关联表)】的数据库操作Service实现
* @createDate 2024-10-12 23:17:48
*/
@Service
public class UserTeamServiceImpl extends ServiceImpl<UserTeamMapper, UserTeam>
    implements UserTeamService{


    @Autowired
    UserService userService;


    @Override
    public int countUser(Long teamId) {
        QueryWrapper<UserTeam> userTeamQueryWrapper1 = new QueryWrapper<>();
        userTeamQueryWrapper1.eq("teamId",teamId);
        return (int) this.count(userTeamQueryWrapper1);
    }

    @Override
    public int countTeam(Long userId) {
        QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
        userTeamQueryWrapper.eq("userId",userId);
        return (int) this.count(userTeamQueryWrapper);
    }

    @Override
    public List<UserVO> listUser(Long teamId) {
        if(teamId == null || teamId < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入队伍id不正确");
        }
        QueryWrapper<UserTeam> userTeamQueryRequestWrapper = new QueryWrapper<>();
        userTeamQueryRequestWrapper.eq("teamId",teamId);
        // 同一队伍里的用户id
        List<UserTeam> list = this.list(userTeamQueryRequestWrapper);
        List<UserVO> userVOS = list.stream().map(userTeam -> {
            UserVO userVO = new UserVO();
            Long userId1 = userTeam.getUserId();
            if(userId1 == null) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户 id 为空");
            }
            User user = userService.getById(userId1);
            BeanUtils.copyProperties(user, userVO);
            return userVO;
        }).collect(Collectors.toList());
        return userVOS;
    }

    @Override
    public List<Long> listTeam(Long userId) {
        if(userId == null || userId < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入用户id不正确");
        }
        QueryWrapper<UserTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        List<UserTeam> userTeams = this.list(queryWrapper);
        if(userTeams == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户未加入任何队伍");
        }
        List<Long> collect = userTeams.stream().map(UserTeam::getTeamId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(collect)){
            return new ArrayList<>();
        }
        return collect;
    }

    @Override
    public UserTeam find(Long teamId, Long userId) {
        QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
        userTeamQueryWrapper.eq("userId",userId);
        userTeamQueryWrapper.eq("teamId",teamId);
        UserTeam userTeam = this.getOne(userTeamQueryWrapper);
        return userTeam ;
    }

    @Override
    public UserTeam findByJoinTime(Long teamId, int current) {
        QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
        userTeamQueryWrapper.eq("teamId",teamId);
        userTeamQueryWrapper.orderByAsc("joinTime");
        List<UserTeam> list = this.list(userTeamQueryWrapper);
        if(CollectionUtil.isEmpty(list) || list.size() < 2){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return list.get(current-1);
    }
}




